package com.stylefeng.guns.rest.modular.version.controller;

import com.stylefeng.guns.rest.common.CommonResponse;
import com.stylefeng.guns.rest.common.exception.BizExceptionEnum;
import com.stylefeng.guns.rest.common.exception.BussinessException;
import com.stylefeng.guns.rest.common.persistence.model.Version;
import com.stylefeng.guns.rest.common.persistence.model.VersionIos;
import com.stylefeng.guns.rest.common.utils.TextUtil;
import com.stylefeng.guns.rest.common.utils.VersionUtils;
import com.stylefeng.guns.rest.modular.version.controller.dto.VersionRequest;
import com.stylefeng.guns.rest.modular.version.service.VersionService;
import com.stylefeng.guns.rest.modular.viewrecord.controller.dto.RecordListRequest;
import com.stylefeng.guns.rest.modular.viewrecord.controller.dto.ViewRecordRequest;
import com.stylefeng.guns.rest.modular.viewrecord.service.ViewRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by ASUS-PC on 2018/6/28.
 */
@RestController
@RequestMapping("/version")
public class VersionController {

    @Autowired
    VersionService versionService;


    //查询版本记录
    @RequestMapping(value = "/getVersionInfo", method = RequestMethod.POST)
    public ResponseEntity<?> queryVersionInfo(@RequestBody VersionRequest request) {
        if(TextUtil.isEmpty(request.getVersion())){
            throw new BussinessException(BizExceptionEnum.NEED_PARAMS);
        }
        Version dev = versionService.getVersion();
        if(dev==null){
            return ResponseEntity.ok(CommonResponse.ok(null));
        }
        if(VersionUtils.compareVersion(request.getVersion(),dev.getVersionNum())){
            return ResponseEntity.ok(CommonResponse.ok(dev));
        }
        return ResponseEntity.ok(CommonResponse.ok(null));

    }

    @RequestMapping(value = "/getVersionIos", method = RequestMethod.POST)
    public ResponseEntity<?> queryVersionIos(@RequestBody VersionRequest request) {
        List<VersionIos> list = versionService.getVersionIos();
        if(list!=null && list.size()>0){
            return ResponseEntity.ok(CommonResponse.ok(list.get(0)));
        }else {
            return ResponseEntity.ok(CommonResponse.ok(null));
        }

    }

}
